local wuchang = fk.CreateSkill {

  name = "joyex__wuchang",

  tags = {  },

}



wuchang:addEffect("active", {
  name = "joyex__wuchang",
  target_num = 1,
  prompt = "#joyex__wuchang",
  can_use = function(self, player)
    return  player:usedSkillTimes(wuchang.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
      return to_select ~= self.id
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    if not to.dead then
      to:drawCards(1,wuchang.name)
    end
    if to.kingdom ~= player.kingdom then
      room:changeKingdom(player, to.kingdom, true)
    end
    local ids = player.room:getCardsFromPileByRule("slash|.|.", 1, "DrawPile")
    if #ids > 0 then
      player.room:obtainCard(player, ids[1], true, fk.ReasonPrey)
    end
  end,
})

wuchang:addEffect(fk.DamageCaused, {
  name = "#joyex__wuchang_trigger",
  anim_type = "defensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(wuchang.name) and target == player and data.to.kingdom == player.kingdom and data.to:getMark("@@joyex__wuchang-phase") == 0 and data.card and (data.card.trueName == "slash"  or data.card.name == "duel" )
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player,wuchang.name,data,"#joyex__wuchang2::"..data.to.id) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.damage = data.damage + 1
    local kingdom = {"shu","wei","wu","qun"}
    local king = {}
    for _, ki in ipairs(kingdom) do
      if player.kingdom ~= ki then
        table.insert(king,ki)
      end
    end
    local k = room:askForChoice(player,king,wuchang.name,"#joyex__wuchang-kingdom")
    room:changeKingdom(player, k, true)
  end,
})
wuchang:addEffect(fk.CardUsing, {
  refresh_events = {fk.CardUsing, fk.CardResponding,fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return target == player and player:hasSkill(wuchang.name) and (data.card.trueName == "slash"  or data.card.name == "duel" )
    else
      return target.kingdom == player.kingdom and target ~= player and player:hasSkill(wuchang.name) and data.responseToEvent and data.responseToEvent.from == player.id and (data.responseToEvent.card.trueName == "slash" or data.responseToEvent.card.name == "duel")
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joyex__wuchang-phase") > 0 then
          room:setPlayerMark(p,"@@joyex__wuchang-phase",0)
        end
      end
    else
      room:setPlayerMark(target,"@@joyex__wuchang-phase",1)
    end
  end,
})
wuchang:addEffect(fk.CardResponding, {
  refresh_events = {fk.CardUsing, fk.CardResponding,fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return target == player and player:hasSkill(wuchang.name) and (data.card.trueName == "slash"  or data.card.name == "duel" )
    else
      return target.kingdom == player.kingdom and target ~= player and player:hasSkill(wuchang.name) and data.responseToEvent and data.responseToEvent.from == player.id and (data.responseToEvent.card.trueName == "slash" or data.responseToEvent.card.name == "duel")
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joyex__wuchang-phase") > 0 then
          room:setPlayerMark(p,"@@joyex__wuchang-phase",0)
        end
      end
    else
      room:setPlayerMark(target,"@@joyex__wuchang-phase",1)
    end
  end,
})
wuchang:addEffect(fk.CardUseFinished, {
  refresh_events = {fk.CardUsing, fk.CardResponding,fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return target == player and player:hasSkill(wuchang.name) and (data.card.trueName == "slash"  or data.card.name == "duel" )
    else
      return target.kingdom == player.kingdom and target ~= player and player:hasSkill(wuchang.name) and data.responseToEvent and data.responseToEvent.from == player.id and (data.responseToEvent.card.trueName == "slash" or data.responseToEvent.card.name == "duel")
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joyex__wuchang-phase") > 0 then
          room:setPlayerMark(p,"@@joyex__wuchang-phase",0)
        end
      end
    else
      room:setPlayerMark(target,"@@joyex__wuchang-phase",1)
    end
  end,
})

return wuchang